package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Emp;
import com.example.mapper.EmpMapper;
import com.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:
 * @Description:
 * @Date: 2019/12/23 17:54
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    @Cacheable(cacheNames = "users", key = "'#empList'")
    public List<Emp> listAll() {
        Wrapper<Emp> empWrapper = new QueryWrapper<>();
        List<Emp> emps = empMapper.selectList(empWrapper);
        System.out.println("去数据库查了。。。");
        return emps;
    }

    @Override
    @CacheEvict(cacheNames = "users", key = "'#empList'")
    public String empDel(String id) {
        empMapper.deleteById(id);
        System.out.println("delete...");
        return "del";
    }

    @Override
    @Cacheable(cacheNames = "emp", key = "'emp_'+#id")
    public Emp getEmpId(String id) {
        Emp emp = empMapper.selectById(id);
        System.out.println("selectById...");

        return emp;
    }

    @Override
    @CachePut(cacheNames = "emp", key = "'emp_'+#id")
    public Emp empUpdate(String id) {
        Emp emp = empMapper.selectById(id);
        System.out.println("修改前。。。");
        System.out.println(emp.toString());
        emp.setEmpName("wahaha");
        System.out.println("修改后。。。");
        System.out.println(emp.toString());
        empMapper.updateById(emp);
        return emp;
    }
}
